package com.jingdianjichi.auth.common.entity;


import lombok.Data;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * @Description: 分页返回的实体
 * @return:
 * @Author:  sky
 * @date:  2024/11/8 16:41
 */
@Data
public class PageResult<T> implements Serializable {

    private Integer pageNo =1;

    private Integer pageSize = 20;

    private Integer total = 0;

    private Integer totalPages = 0;

    private List<T> result = Collections.emptyList();

    private Integer start = 1;

    private Integer end = 0;

    public void setRecords(List<T> result){
        this.result = result;
//        if(result!=null && result.size()>0){
//            setTotal(result.size());
//
//        }
    }

    public void setTotal(Integer total) {

        // 计算总记录数total 、 总页数 totalPages
        this.total = total;
        if(pageSize>0){
            this.totalPages = (total / this.pageSize) + (total % this.pageSize ==0? 0:1);
        }
        else{
            this.totalPages =0 ;
        }

        // 计算目标页的 起始地址（起始记录） 、终点地址（记录）
        this.start = (this.pageSize > 0  ? (this.pageNo -1) * this.pageSize : 0 ) + 1;
        this.end = this.start - 1 + this.pageSize * (this.pageSize >0 ? 1 : 0);
    }

    public void setPageSize(Integer pageSize){
        this.pageSize = pageSize;
    }

    public void setPageNo(Integer pageNo){
        this.pageNo = pageNo;
    }
}
